<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/22
 * Time: 13:58
 */
namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Api\WeChatControllerBase;
use App\Modules\User\Models\User;
use Illuminate\Http\Request;
use JWTAuth;
use JWTFactory;
use Namshi\JOSE\JWT;
use Redis;
use TencentCloud\Faceid\V20180301\Models\GetDetectInfoRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Faceid\V20180301\FaceidClient;
use TencentCloud\Faceid\V20180301\Models\DetectAuthRequest;


class AuthController extends WeChatControllerBase {
    /**
     * @apiDefine Auth 授权
     */
    /**
     * @apiVersion 1.0.0
     * @apiDescription 用户授权，用token换取uid
     * @api {POST} v1/auth/get_token 获取用户授权信息
     * @apiParam   {string}  code 授权时要用到的code参数
     * @apiGroup Auth
     * @apiName index
     * @apiSuccess {string}  token   用户token
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 200,
     *         "msg": "",
     *         "data": {'token':'COVBZ-OVL6O-L4WWD-SBO4P-BVB22-LLB3U'}
     *     }
     */
    public function index(Request $request)
    {

        if(!$request->has('code')) {
            return response()->error('PARAM_EMPTY');
        }

        $code = $request->input('code');
        //Log::error('code1:'.$code);
        $result = $this->app->auth->session($code);
        //\Log::info($result);
        //$user = \App\Modules\User\Models\User::addOrUpdate(['openid'=>$result['openid']]);
        $user = User::getUserByOpenid($result['openid']);
        if(empty($user)) {
            $user = User::create(['openid'=>$result['openid'],'nickname'=>'User_'.rand(0,99999),'avatar'=>env('DEFAULT_AVATAR')]);
        }
        $expire_at = time() + 43200*60;
        $token = JWTAuth::fromUser($user);
        $data = array(
            'token' => $token,
            'session_key'=>$result['session_key'],
            'expire_at'=>$expire_at
        );
        
        return response()->success($data);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function DetectAuth(Request $request)
    {
        try {

            $cred = new Credential("", "");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("faceid.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new FaceidClient($cred, "", $clientProfile);

            $req = new DetectAuthRequest();

            $params = array(
            );
            $req->fromJsonString(json_encode($params));


            $resp = $client->DetectAuth($req);

            print_r($resp->toJsonString());
            return response()->success($resp);
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function GetDetectInfo(Request $request) {
        try {

            $cred = new Credential("", "");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("faceid.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new FaceidClient($cred, "", $clientProfile);

            $req = new GetDetectInfoRequest();

            $params = array(
            );
            $req->fromJsonString(json_encode($params));


            $resp = $client->GetDetectInfo($req);

            print_r($resp->toJsonString());
            return response()->success($resp);
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }
}